package edu.uestc.fly.performancetest.io;

import android.content.Context;
import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.uestc.fly.performancetest.constant.Constant;
import edu.uestc.fly.performancetest.utils.ProgressListen;

/**
 * Created by flyln on 2017/10/23 15:42.
 * email:xyf_uestc@163.com
 *
 * String*********对应我们的URL类型
 * Integer********进度条的进度值
 * BitMap*********异步任务完成后返回的类型
 * 
 */
public class IOAsyncTask extends AsyncTask<String, Integer, Double> {

    public Context context;
    public ProgressListen progresslistener;
    public Constant.TaskType taskType = Constant.TaskType.IO;
    private DBManager dbManager;


    public IOAsyncTask(Context context){
        this.context = context;
        dbManager = new DBManager(context);
    }

    //执行异步任务（doInBackground）之前执行，并且在ui线程中执行
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


        if (progresslistener != null){
            progresslistener.initProgress(taskType,0);
        }


    }

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub



        int size =1000;
        int i,length = size;
        char oldArray[] = new char[size];
        long StartTime = System.currentTimeMillis();

        for (i = 0 ; i < size ; i ++ ){
            add();
            update();
            query();
            delete();

            publishProgress((int)( (float)i / (float)size * 100 ));
        }

        long EndTime = System.currentTimeMillis();

        System.out.println("消耗时间: " + (EndTime - StartTime)+" ms.");




        return (double)EndTime - StartTime;
    }
    //在ui线程中执行 可以操作ui
    @Override
    protected void onPostExecute(Double runTime) {
        // TODO Auto-generated method stub
        super.onPostExecute(runTime);
        //任务完成，对话框进度条隐藏

        if (progresslistener != null){
            progresslistener.closeProgress(taskType,runTime);
        }
    }
    /*
     * 在doInBackground方法中已经调用publishProgress方法 更新任务的执行进度后
     * 调用这个方法 实现进度条的更新
     * */
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        if (progresslistener != null){
            progresslistener.updateProgress(taskType,values[0]);
        }
    }

    public void add()
    {
        ArrayList<Person> persons = new ArrayList<Person>();


        for (int i = 0 ; i < 10 ; i++){
            persons.add(new Person("Ella"+i, 12+i, "lively girl"));
        }


        dbManager.add(persons);
    }

    public void update()
    {
        // 把%Ella%的年龄改为30（注意更改的是数据库中的值，要查询才能刷新ListView中显示的结果）
        Person person = new Person();
        person.name = "%Ella%";
        person.age = 30;
        dbManager.updateAge(person);
    }

    public void delete()
    {
        // 删除所有三十岁以上的人（此操作在update之后进行，Jane会被删除（因为她的年龄被改为30））
        // 同样是查询才能查看更改结果
        Person person = new Person();
        person.age = 30;
        dbManager.deleteOldPerson(person);
    }

    public void query()
    {
        List<Person> persons = dbManager.query();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (Person person : persons)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", person.name);
            map.put("info", person.age + " years old, " + person.info);
            list.add(map);
        }

    }

}